/**
 * 根据fis生成的map文件过滤出地址
 * @param data
 * @returns {Array|*}
 */

var cp = require("child_process"),
    path = require("path"),
    fs = require("fs"),
    prefix = path.resolve(__dirname, "../../") + "/.output",
    logTimer;

exports.PREFIX_PATH = prefix;

exports.filterResources = function (data) {
    var _ = require("underscore"),
        depolyInfo = require("../../deploy-conf.json"),
        types = ["js", "css", "less", "png", "gif", "jpg", "webp"],
        domains = depolyInfo.domains,
        tmp = _.clone(data),
        maps = [];

    tmp = _.filter(tmp, function (item) {
        if (types.indexOf(item.type) != -1) {
            return true;
        }
        return false;
    });

    _.each(domains, function (domain) {
        _.each(tmp, function (item) {
            item.uri = item.uri.slice(domain.length);
            maps.push({
                path: prefix + item.uri,
                filename: item.uri
            });
        });
    });

    return maps;
};

exports.getAllFilePath = function (path, files) {
    var self = this,
        tmp = fs.readdirSync(path),
        exclude = ["autoload.php", "installed.json"],
        item, i, len, stat, tmpPath;

    for (i = 0, len = tmp.length; i < len; i++) {
        item = tmp[i];
        tmpPath = path + "/" + item;
        stat = fs.statSync(tmpPath);

        if (stat.isFile() && exclude.indexOf(item) == -1) {
            files.push({
                dist: path.slice(path.lastIndexOf("/vendor")) + "/" + item,
                filename: item,
                path: tmpPath
            });
        } else if (stat.isDirectory()) {
            self.getAllFilePath(tmpPath, files);
        }
    }
};

exports.getAllFiles = function (path, reg) {
    var fileItems = [];

    if (fs.existsSync(path)) {
        var state = fs.lstatSync(path);
        if (state.isDirectory()) {
            var files = fs.readdirSync(path);

            for (var i = 0, len = files.length; i < len; i++) {
                var filePath = path + "/" + files[i];
                state = fs.lstatSync(filePath);
                if (state.isDirectory()) {
                    fileItems = fileItems.concat(this.getAllFiles(filePath, reg));
                } else if (reg.test(filePath)) {
                    fileItems.push({
                        path: filePath,
                        filename: filePath.slice(prefix.length)
                    });
                }
            }
        } else if (reg.test(path)) {
            fileItems.push({
                path: path,
                filename: "/" + path.slice(path.lastIndexOf("/") + 1)
            });
        }
    }

    return fileItems;
};

exports.findIn = function (item, arr, key) {
    for (var i = 0, len = arr.length; i < len; i++) {
        if (arr[i][key] == item) {
            return i;
        }
    }

    return -1;
};

exports.startWaitLog = function () {
    var self = this;

    logTimer = setTimeout(function () {
        self.log(".");
        self.startWaitLog();
    }, 1000);
};

exports.stopWaitLog = function () {
    clearTimeout(logTimer);
};

exports.log = function (logStr) {
    process.stdout.write(logStr);
};

exports.getCmdValue = function (argvArr, cmd) {
    var self = this,
        tmp = [],
        value = 0,
        i, len, item, index;

    if (cmd.length < 2) {
        return 2;
    }

    cmd = cmd.splice(2);

    for (i = 0, len = cmd.length; i < len; i++) {
        item = cmd[i];

        if (/^-/.test(item)) {
            item = item.slice(1).split("");

            if (item.length <= 1) {
                getParams(i + 1, item);
            } else {
                item.forEach(function (c, k) {
                    getParams(i + k + 1, c);
                });
            }

            tmp = tmp.concat(item);
        }
    }

    tmp.forEach(function (item) {
        if ((index = self.findIn(item, argvArr, 'cmd')) != -1) {
            value = value | Math.pow(2, index);
        }
    });


    function getParams(k, c) {
        if (k < len && !/^-/.test(cmd[k])) {
            var index = self.findIn(c, argvArr, 'cmd');
            argvArr[index]["params"] = cmd[k].split(",");
        }
    }

    return value || 2;
}